#！/bin/bash
source /etc/profile
if [ -n "$1" ]
then
	do_date=$1
else
	do_date=`date -d "-1 day" +%F`
fi

sql="
insert overwrite table dim.dim_trade_shops
select shopid,
userid,
areaid,
shopname,
shoplevel,
status,
createTime,
modifyTime,
case when modifyTime is not null
then substr(modifyTime, 0, 10)
else substr(createTime, 0, 10)
end as start_dt,
'9999-12-31' as end_dt
from ods.ods_trade_shops
where dt = '$do_date'
 union all 

select dim.shopid,
dim.userid,
dim.areaid,
dim.shopname,
dim.shoplevel,
dim.status,
dim.createTime,
dim.modifyTime,
dim.start_dt,
case when dim.end_dt >='9999-12-31' and ods.shopId is not null 
then date_add('$do_date',-1)
else dim.end_dt end end_dt
from dim.dim_trade_shops  dim
left join (select * from ods.ods_trade_shops where dt='$do_date' )ods
on dim.shopId= ods.shopId;
"
hive -e "$sql"
